#include "include/cppcore.h"
#include "include/all.h"
#include "source/allcpp.cpp"

int main() {
    setlocale(LC_CTYPE, "");
    Printer prt;

    // 读取学生, 课程, 选课数据.

    auto sList = new List<Student>;
    auto cList = new List<Course>;
    auto scList = new List<StuCrs>;
    sList->create();
    cList->create();
    scList->create();

    // 根据读取的结果展示相应界面.
    if (sList->head->next || cList->head->next) {
        prt.printMenu(prt.WELCOME);
    } else {
        prt.printMenu(prt.WELCOME_FIRST);
    }

    while (1) {
        auto sel = prt.printMenu(prt.M_MAIN, {L"0", L"1", L"2", L"3", L"4"});


        // 输入学生和课程信息.
        if (sel == L"1") {
            sel = prt.printMenu(prt.M_1, {L"0", L"1", L"2", L"3"});
            if (sel == L"1") { sList->scan(); }
            if (sel == L"2") { cList->scan(); }
            // 函数实现在 printer.cpp 里.
            if (sel == L"3") { selection(sList, cList, scList); }
            continue;
        }


        // 查询输出信息.
        if (sel == L"2") {
            // 输入条件查询学生, 课程信息
            sel = prt.printMenu(prt.M_2, {L"0", L"1", L"2", L"3"});
            if (sel == L"1") {
                system("cls");
                prt.fmtln(L"查询学生信息\n");
                FIND_STUDENT:
                sList->print();
                sel = sList->scanCommand();
                std::vector<Node<Student>*>* fndVec = sList->search(sel);
                system("cls");
                prt.fmtln(L"********查询结果: ********\n");
                for (auto val : *fndVec) {
                    val->next->data.print();
                }
                prt.fmtln(L"输入1修改这些学生信息, 输入2删除这些学生信息,\n"
                    "输入其他内容取消:");
                std::getline(std::wcin, sel);
                if (sel == L"1") {
                    for (auto p : *fndVec) { p->next->data.rescan(); }
                    system("cls");
                    prt.fmtln(L"输入完成!");
                    for (auto p : *fndVec) { p->next->data.print(); }
                    system("pause");

                } else if (sel == L"2") {
                    // 这里只能用反向迭代器, 正向会导致 vector 里的数据提前失效.
                    for (auto p = fndVec->rbegin(); p != fndVec->rend(); ++p) {
                        (*p)->erase(*p); --sList->counter;
                    }
                    sList->tail = sList->head;
                    while (sList->tail->next) { sList->tail = sList->tail->next; }
                    system("cls");
                    prt.fmtln(L"删除完成!");
                    system("pause");
                }
                delete fndVec;
                continue;
            }

            if (sel == L"2") {
                system("cls");
                prt.fmtln(L"查询课程信息\n");
                FIND_COURSE:
                cList->print();
                sel = cList->scanCommand();
                std::vector<Node<Course>*>* fndVec = cList->search(sel);
                system("cls");
                prt.fmtln(L"********查询结果: ********\n");
                for (auto val : *fndVec) {
                    val->next->data.print();
                }
                prt.fmtln(L"输入1修改这些课程信息, 输入2删除这些课程信息,\n"
                    "输入其他内容取消:");
                std::getline(std::wcin, sel);
                if (sel == L"1") {
                    for (auto p : *fndVec) { p->next->data.rescan(); }
                    system("cls");
                    prt.fmtln(L"输入完成!");
                    for (auto p : *fndVec) { p->next->data.print(); }
                    system("pause");

                } else if (sel == L"2") {
                    // 这里只能用反向迭代器, 正向会导致 vector 里的数据提前失效.
                    for (auto p = fndVec->rbegin(); p != fndVec->rend(); ++p) {
                        (*p)->erase(*p); --cList->counter;
                    }
                    cList->tail = cList->head;
                    while (cList->tail->next) { cList->tail = cList->tail->next; }
                    system("cls");
                    prt.fmtln(L"删除完成!");
                    system("pause");
                }
                delete fndVec;
                continue;
            }

            if (sel == L"3") {
                system("cls");
                prt.fmtln(L"查询选课信息\n");
                FIND_STU_CRS:
                scList->print();
                sel = scList->scanCommand();
                std::vector<Node<StuCrs>*>* fndVec = scList->search(sel);
                system("cls");
                prt.fmtln(L"********查询结果: ********\n");
                for (auto val : *fndVec) {
                    val->next->data.print();
                }
                prt.fmtln(L"输入1退选这些课程, 输入其他内容取消:");
                std::getline(std::wcin, sel);
                if (sel == L"1") {
                    // 这里只能用反向迭代器, 正向会导致 vector 里的数据提前失效.
                    for (auto p = fndVec->rbegin(); p != fndVec->rend(); ++p) {
                        (*p)->erase(*p); --scList->counter;
                    }
                    scList->tail = scList->head;
                    while (scList->tail->next) { scList->tail = scList->tail->next; }
                    system("cls");
                    prt.fmtln(L"删除完成!");
                    system("pause");
                }
                delete fndVec;
                continue;
            }
            continue;
        }


        // 修改数据.
        if (sel == L"3") {
            sel = prt.printMenu(prt.M_3, {L"0", L"1", L"2", L"3"});
            if (sel == L"1") {
                system("cls");
                prt.fmtln(L"修改指定学生请先进行查询功能.\n");
                goto FIND_STUDENT;
            }
            if (sel == L"2") {
                system("cls");
                prt.fmtln(L"修改指定课程请先进行查询功能.\n");
                goto FIND_COURSE;
            }
            if (sel == L"3") {
                system("cls");
                prt.fmtln(L"退课请先进行查询功能.\n");
                goto FIND_STU_CRS;
            }
            continue;
        }


        // 清空链表删除数据.
        if (sel == L"4") {
            sel = prt.printMenu(prt.M_4, {L"0", L"1", L"2", L"3", L"4"});
            if (sel != L"0") {
                std::wstring check;
                if (sel == L"1") check = L"学生";
                if (sel == L"2") check = L"课程";
                if (sel == L"3") check = L"选课";
                if (sel == L"4") check = L"所有";
                prt.fmtln(L"确定要清除{}数据吗? 请输入\"删除\"进行确认!", check);
                getline(std::wcin, check);
                if (check != L"删除") {
                    prt.fmtln(L"没有进行操作");
                } else {
                    if (sel == L"1") { sList->clear(); }
                    if (sel == L"2") { cList->clear(); }
                    if (sel == L"3") { scList->clear(); }
                    if (sel == L"4") {
                        sList->clear();
                        cList->clear();
                        scList->clear();
                    }
                    prt.fmtln(L"已清空指定数据!");
                }
                system("pause");
            }
            continue;
        }


        // 写入数据到文件并退出.
        if (sel == L"0") {
            std::filesystem::create_directory("data");
            // system("if not exist \"./data\" mkdir data");
            sList->save();
            cList->save();
            scList->save();
            return 0;
        }
    }

    return 0;
}
